package com.xuecheng.learning.domain.model.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.xuecheng.learning.domain.model.enums.XcCourseOrderEnum;
import com.xuecheng.learning.domain.model.enums.XcCourseStatusEnum;
import com.xuecheng.learning.domain.model.vo.resp.XcChooseCourseResp;
import com.xuecheng.learning.infrastructure.vo.CoursePublish;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * <p>
 * 
 * </p>
 *
 * @author itcast
 */
@Data
@TableName("xc_choose_course")
public class XcChooseCourse implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 课程id
     */
    private Long courseId;

    /**
     * 课程名称
     */
    private String courseName;

    /**
     * 用户id
     */
    private String userId;

    /**
     * 机构id
     */
    private Long companyId;

    /**
     * 选课类型
     */
    private String orderType;

    /**
     * 添加时间
     */
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createDate;

    /**
     * 课程价格
     */
    private Float coursePrice;

    /**
     * 课程有效期(天)
     */
    private Integer validDays;

    /**
     * 选课状态
     */
    private String status;

    /**
     * 开始服务时间
     */
    private LocalDateTime validtimeStart;

    /**
     * 结束服务时间
     */
    private LocalDateTime validtimeEnd;

    /**
     * 备注
     */
    private String remarks;


    public static XcChooseCourse buildCourse(Long userId, CoursePublish coursepublish, String orderType, String status) {
        LocalDateTime now = LocalDateTime.now();
        XcChooseCourse xcChooseCourse = new XcChooseCourse();
        xcChooseCourse.setCourseId(coursepublish.getId());
        xcChooseCourse.setCourseName(coursepublish.getName());
        xcChooseCourse.setUserId(userId.toString());
        xcChooseCourse.setCompanyId(coursepublish.getCompanyId());
        xcChooseCourse.setOrderType(orderType);
        xcChooseCourse.setCreateDate(coursepublish.getCreateDate());
        xcChooseCourse.setCoursePrice(coursepublish.getPrice());
        xcChooseCourse.setValidDays(orderType.equalsIgnoreCase(XcCourseOrderEnum.FREE.getCode()) ? 365 : coursepublish.getValidDays()); // 免费课程默认一年
        xcChooseCourse.setValidtimeStart(now);
        xcChooseCourse.setValidtimeEnd(now.plusDays(xcChooseCourse.getValidDays()));
        xcChooseCourse.setStatus(status);
        return xcChooseCourse;
    }
}
